<template>
  <div class="container">
    <div id="operation">

      <vcrontab @hide="showCron=false"
                @create="createInventoryMerge"
                @update="updateInventoryMerge"
                :expression="this.form.expression"
                :disAbleUpdate="!this.canUpdate"
                :disAbleCreate="!this.canCreate"
                style="width: 888px"></vcrontab>

      <div id="more-operation" align="center">
        <el-button type="success"
                   size="small"
                   @click="resume"
                   :disabled="!this.canDo"
        >恢复
        </el-button>

        <el-button type="warning"
                   size="small"
                   @click="pause"
                   :disabled="!this.canPause"
        >暂停
        </el-button>

        <el-button type="danger"
                   size="small"
                   @click="runOnce"
                   :disabled="!this.canDoOnce"
        >执行
        </el-button>
      </div>

    </div>
  </div>
</template>

<script>
import vcrontab from '@/components/MyCron/Crontab'

import {
  getSortInventoryQuartzBean,
  createSortInventoryJob,
  pauseSortInventory,
  resumeSortInventory,
  runOnceSortInventory,
  updateSortInventoryCron,
  inventoryMerge
} from '@/api/user'

export default {
  components: {vcrontab},
  created() {
    this.getScheduleInfo()
  },
  data() {
    return {
      form: {
        expression: ''
      },
      status: '',
      canUpdate: false,
      canCreate: false,
      canDo: false,
      canPause: false,
      canResume: false,
      canDoOnce: false
    }
  },
  methods: {
    getScheduleInfo() {
      getSortInventoryQuartzBean().then(res => {
        this.form.expression = res.data.cronExpression
        this.status = res.data.status
        this.judgeButton()
        console.log(this.status)
      })
    },
    judgeButton() {
      if (this.status == 0) { // 未创建
        this.canCreate = true
        this.canUpdate = false
        this.canDo = false
        this.canPause = false
        this.canDoOnce = false
      } else if (this.status == 1) { // 运行中
        this.canCreate = false
        this.canUpdate = true
        this.canDo = false
        this.canPause = true
        this.canDoOnce = true
      } else { // 暂停中
        this.canCreate = false
        this.canUpdate = true
        this.canDo = true
        this.canPause = false
        this.canDoOnce = true
      }
    },
    // setExpression(value) {
    //   var map = {
    //     expression: value
    //   }
    //   setInventoryCron(map).then(res => {
    //     if (res.data) {
    //       this.$message({
    //         message: '修改成功',
    //         type: 'success'
    //       })
    //     } else {
    //       this.$message({
    //         message: '修改失败',
    //         type: 'error'
    //       })
    //     }
    //   })
    // },
    pause() {
      pauseSortInventory().then(res => {
        if (res.data) {
          this.$message({
            message: '定时任务已暂停',
            type: 'success'
          })
          this.status = 2
          this.judgeButton()
        } else {
          this.$message({
            message: '暂停失败',
            type: 'error'
          })
        }
      })
    },
    resume() {
      resumeSortInventory().then(res => {
        if (res.data) {
          this.$message({
            message: '定时任务已恢复',
            type: 'success'
          })
          this.status = 1
          this.judgeButton()
        } else {
          this.$message({
            message: '恢复失败',
            type: 'error'
          })
        }
      })
    },
    runOnce() {
      runOnceSortInventory().then(res => {
        if (res.data) {
          this.$message({
            message: '执行成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '执行失败',
            type: 'error'
          })
        }
      })
    },
    updateInventoryMerge(value) {
      var map = {
        expression: value
      }
      updateSortInventoryCron(map).then(res => {
        if (res.data) {
          this.$message({
            message: '更新成功,已恢复运行',
            type: 'success'
          })
          this.status=1
          this.judgeButton()
        } else {
          this.$message({
            message: '更新失败',
            type: 'error'
          })
        }
      })
    },
    createInventoryMerge(value) {
      var map = {
        expression: value
      }
      createSortInventoryJob(map).then(res => {
        if (res.data) {
          this.$message({
            message: '创建成功',
            type: 'success'
          })
          this.status=1
          this.judgeButton()
        } else {
          this.$message({
            message: '创建失败',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  padding: 50px;
  display: flex;
  justify-content: center;

  .form {
    width: 700px;
  }
}
</style>
